﻿using AntdUI;
using ImportPlcPointPosition.Controls.Pages;
using ImportPlcPointPosition.Controls.Base;
using ImportPlcPointPosition.Forms;
using ImportPlcPointPosition.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImportPlcPointPosition
{
    public partial class MainLayout : BorderlessForm
    {
        private Dictionary<string, object> dicControls = new Dictionary<string, object>();

        public MainLayout()
        {
            InitializeComponent();
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = windowBar1.Text;
            this.Icon = ImageToIcon(windowBar1.Icon);
            this.Load += Main_Load;
        }

        #region Methods

        private void Main_Load(object sender, EventArgs e)
        {
            AntdUI.Config.IsLight = true;
            AntdUI.Config.ScrollBarHide = true;
            AntdUI.Config.ShowInWindow = true;

            App.MainLayout = this;

            this.leftMenu.SelectIndex(-1);
            this.leftMenu.SelectChanged += LeftMenu_SelectChanged;
            this.leftMenu.SelectIndex(0);
            this.FormClosing += MainLayout_FormClosing;

        }

        public Icon ImageToIcon(Image image)
        {
            // 创建一个与原始图像大小相同的空白图像
            Bitmap iconBitmap = new Bitmap(64, 64);
            // 使用原始图像绘制空白图像
            using (Graphics graphics = Graphics.FromImage(iconBitmap))
            {
                graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.DrawImage(image, 0, 0, 64, 64);
            }
            using (Icon icon = Icon.FromHandle(iconBitmap.GetHicon()))
            {
                return Icon.FromHandle(icon.Handle);
            }
        }

        #endregion

        #region Events

        private void MainLayout_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (dicControls.ContainsKey("pointConfig"))
            {
                var dpp = dicControls["pointConfig"] as DataPositionPage;
                dpp.DisConnectPlc();
            }
        }

        private void LeftMenu_SelectChanged(object sender, MenuSelectEventArgs e)
        {
            var name = e.Value.ID;
            var control = new BaseUserControl();
            pnl_Content.Controls.Clear();
            if (!dicControls.ContainsKey(name.ToString()))
            {
                switch (name)
                {
                    case "plcConfig":
                        control = new PlcManagePage();
                        break;
                    case "blockDbConfig":
                        control = new PlcBlockPage();
                        break;
                    case "pointConfig":
                        control = new DataPositionPage();
                        break;
                    case "collectDataConfig":
                        control = new CollectDataConfigPage();
                        break;
                    case "techniqueTemplate":
                        control = new TechniqueTemplatePage();
                        break;
                    case "techniqueRoad":
                        control = new TechnicalFlowPage();
                        break;
                    default:
                        break;
                }
                //设置控件默认主题
                control.BackColor = Dark ? Color.Black : Color.White;
                control.ForeColor = Dark ? Color.White : Color.Black;
                dicControls[name.ToString()] = control;
            }
            if (dicControls.ContainsKey(name.ToString()))
            {
                var ctl = dicControls[name.ToString()] as Control;
                //容器添加控件，需要调整dpi
                ctl.Dock = DockStyle.Fill;
                //AutoDpi(control);
                pnl_Content.Controls.Add(ctl);
                if (ctl is BaseUserControl)
                    (ctl as BaseUserControl).ReflushPage();
            }
        }

        /// <summary>
        /// 主题切换
        /// </summary>
        bool setcolor = false;
        private void btnMode_Click(object sender, EventArgs e)
        {
            if (setcolor)
            {
                var color = AntdUI.Style.Db.Primary;
                AntdUI.Config.IsDark = !AntdUI.Config.IsDark;
                Dark = AntdUI.Config.IsDark;
                AntdUI.Style.SetPrimary(color);
            }
            else
            {
                AntdUI.Config.IsDark = !AntdUI.Config.IsDark;
                Dark = AntdUI.Config.IsDark;
            }
            btnMode.Toggle = Dark;
            this.BackColor = Dark ? Color.Black : Color.White;
            this.ForeColor = Dark ? Color.White : Color.Black;
            foreach (var item in dicControls)
            {
                var ctl = (item.Value as BaseUserControl);
                ctl.BackColor = Dark ? Color.Black : Color.White;
                ctl.ForeColor = Dark ? Color.White : Color.Black;
            }
            OnSizeChanged(e);
        }

        #endregion
    }
}
